package com.haitaoit.chengquan.module.home.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.haitaoit.chengquan.R;
import com.haitaoit.chengquan.base.BackCall;
import com.haitaoit.chengquan.module.home.model.CreditSearchModel;
import com.haitaoit.chengquan.view.CircleImageView;
import com.haitaoit.chengquan.view.QRatingBar;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;

/**
 * Created by Administrator on 2016/10/20.
 */
public class QCreditSearchAdapter extends BaseAdapter {
    private Context context;
    private BackCall backCall;
    private List<CreditSearchModel> list;

    public void setBackCall(BackCall backCall) {
        this.backCall = backCall;
    }

    public QCreditSearchAdapter(Context context) {
        this.context = context;
    }

    public List<CreditSearchModel> getList() {
        return list;
    }

    public void setList(List<CreditSearchModel> list) {
        this.list = list;
    }

    @Override
    public int getCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_credit_search, null);
            holder = new ViewHolder();
            ViewUtils.inject(holder, convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        ImageLoader.getInstance().displayImage(list.get(position).getHead_portrait(), holder.center_img);
        holder.tv_title.setText(list.get(position).getReal_name());
        holder.got_out_of_line.setText("不良记录为"+list.get(position).getNum()+"次");
        holder.eva_star.setRating(Float.valueOf(list.get(position).getCredit_rating()));
        if (list.get(position).getUser_type().equals("1")) {// user_type:1需求商2服务商
            holder.tv_content.setText("账户类型:需求商");
        } else {
            holder.tv_content.setText("账户类型:服务商");

        }

        holder.brand_itemll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                QCreditSearchAdapter.this.backCall.deal(R.id.brand_itemll, position);
            }
        });
        holder.got_out_of_line.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                QCreditSearchAdapter.this.backCall.deal(R.id.got_out_of_line, position);
            }
        });
        return convertView;
    }

    class ViewHolder {
        @ViewInject(R.id.brand_itemll)
        LinearLayout brand_itemll;
        @ViewInject(R.id.center_img)
        CircleImageView center_img;
        @ViewInject(R.id.tv_title)
        TextView tv_title;
        @ViewInject(R.id.tv_content)
        TextView tv_content;
        @ViewInject(R.id.eva_star)
        QRatingBar eva_star;
        @ViewInject(R.id.got_out_of_line)
        TextView got_out_of_line;

    }
}
